<?php

namespace app\master\controller;

use think\Db;
use think\Exception;

class Friend extends Base
{
    //单位
    public function company()
    {
        if (request()->isGet()) {
            return $this->fetch("company");
        }
        $page = input("page");
        $limit = input("limit");
        $list = model('Company')->company($page, $limit);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //添加单位
    public function addcompany()
    {
        $this->view->engine->layout(false);
        if (request()->isGet()) {
            return $this->fetch("addcompany");
        }
        $data = input('post.');
        if (empty($data['company_name'])) $this->ajaxReturn(603, "请填写单位名称");
        if (empty($data['company_people'])) $this->ajaxReturn(603, "请填写单位人数");
        if (empty($data['company_address'])) $this->ajaxReturn(603, "请填写单位地址");
        $company = [
            "company_name" => $data['company_name'],
            "company_people" => $data['company_people'],
            "company_address" => $data['company_address'],
            "company_time" => time(),
            "company_status" => 1,
            "pid" => 0,
        ];
        $update = db("company")->insert($company);
        if ($update) {
            $this->ajaxReturn(200, "添加成功");
        } else {
            $this->ajaxReturn(603, "添加失败");
        }
    }

    //禁用单位
    public function down()
    {
        $company_id = input('company_id');
        Db::startTrans();
        try {
            //禁用单位下属部门变状态，页面看不到，用户变状态，只能看不能约车
            db('company')->where('company_id', $company_id)->update(['company_status' => 2]);
            db('department')->where('company_id', $company_id)->update(['department_status' => 3]);
            db('member')->where('company_id', $company_id)->update(['member_ifcar' => 2]);
            Db::commit();
            $this->ajaxReturn(200, successMsg);
        } catch (\Exception $e) {
            Db::rollback();
            $this->ajaxReturn(603, errorMsg);
        }
    }

    //启用单位
    public function open()
    {
        $company_id = input('company_id');
        Db::startTrans();
        try {
            //启用单位下属部门变状态，页面看到，用户变状态，能看能约车
            db('company')->where('company_id', $company_id)->update(['company_status' => 1]);
            db('department')->where('company_id', $company_id)->update(['department_status' => 1]);
            db('member')->where('company_id', $company_id)->update(['member_ifcar' => 1]);
            Db::commit();
            $this->ajaxReturn(200, successMsg);
        } catch (\Exception $e) {
            Db::rollback();
            $this->ajaxReturn(603, errorMsg);
        }
    }

    //删除单位
    public function delcompany()
    {
        $company_id = input('company_id');
        Db::startTrans();
        try {
            //删除单位下属部门变状态，页面看不到，用户变状态，能看不能约车
            db('company')->where('company_id', $company_id)->update(['company_status' => 3]);
            db('department')->where('company_id', $company_id)->update(['department_status' => 3]);
            db('member')->where('company_id', $company_id)->update(['member_ifcar' => 2]);
            Db::commit();
            $this->ajaxReturn(200, successMsg);
        } catch (\Exception $e) {
            Db::rollback();
            $this->ajaxReturn(603, errorMsg);
        }
    }

    //部门
    public function department()
    {
        if (request()->isGet()) {
            return $this->fetch("department");
        }
        $page = input("page");
        $limit = input("limit");
        $list = model('Company')->department($page, $limit);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //添加部门
    public function adddepartment()
    {
        $this->view->engine->layout(false);
        if (request()->isGet()) {
            $company = db('company')->where('company_status', 1)->select();
            $this->assign("company", $company);
            return $this->fetch("adddepartment");
        }
        $department_name = input('department_name');
        $company_id = input('company_id');
        if (empty($company_id)) $this->ajaxReturn(603, "请选择所属单位");
        if (empty($department_name)) $this->ajaxReturn(603, "请填写部门名称");
        $department = [
            "department_name" => $department_name,
            "company_id" => $company_id,
            "department_time" => time(),
            "department_status" => 1,
        ];
        $company = [
            "company_name" => $department_name,
            "company_time" => time(),
            "company_status" => 1,
            "pid" => $company_id,
        ];
        Db::startTrans();
        try {
            //添加部门时在部门表添加数据，并在单位表单位所属下面添加子数据
            $id = db("department")->insertGetId($department);
            $company['department_id'] = $id;
            db('company')->insert($company);
            Db::commit();
            $this->ajaxReturn(200, successMsg);
        } catch (\Exception $e) {
            Db::rollback();
            $this->ajaxReturn(603, errorMsg);
        }
    }

    //删除部门
    public function deldepartment()
    {
        $department_id = input('department_id');
        $list = db('department')->where('department_id', $department_id)->update(['department_status' => 3]);
        if ($list) {
            $this->ajaxReturn(200, successMsg);
        } else {
            $this->ajaxReturn(603, errorMsg);
        }
    }
}